package com.jeff.work;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class CompleteBinaryTree {
    public static TreeNode buildTree(int[] nums) {
        if (nums == null || nums.length == 0) return null;
        return buildTreeHelper(nums, 0);
    }

    private static TreeNode buildTreeHelper(int[] nums, int index) {
        if (index >= nums.length) return null;
        TreeNode node = new TreeNode(nums[index]);
        node.left = buildTreeHelper(nums, 2 * index + 1);
        node.right = buildTreeHelper(nums, 2 * index + 2);
        return node;
    }
    //添加前序遍历代码
    public static void preOrder(TreeNode root) {
        if (root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //添加中序遍历代码
    public static void inOrder(TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    //添加后序遍历代码
    public static void postOrder(TreeNode root) {
        if (root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6};
        TreeNode root = buildTree(nums);
        preOrder(root);
        System.out.println("===========");
        inOrder(root);
        System.out.println("===========");
        postOrder(root);
    }
}
